package com.example.weblending.controlller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.weblending.domain.ApplyNum;
import com.example.weblending.domain.User;
import com.example.weblending.exception.GlobalException;
import com.example.weblending.utils.JwtUtils;
import com.example.weblending.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/apply")
@CrossOrigin
public class ApplyNumController {

    /**
     *  用户申请增加清洗次数
     */
    @PostMapping("clean/{num}")
    public Result applyCleanNum(@PathVariable int num, HttpServletRequest request){
        User user= JwtUtils.checkUser(request,userService);
        if (user.getPermission()!=0){
            throw new GlobalException(403,"您无需申请增加次数");
        }
        boolean success = applyNumService.apply(user,0,num);
        return success?Result.ok():Result.error();
    }

    /**
     *  用户申请增加分析次数
     */
    @PostMapping("analyze/{num}")
    public Result applyAnalyzeNum(@PathVariable int num,HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        if (user.getPermission()!=0){
            throw new GlobalException(403,"您无需申请增加次数");
        }
        boolean success = applyNumService.apply(user,1,num);
        return success?Result.ok():Result.error();
    }

    /**
     * 管理员同意了某一次数申请
     */
    @PostMapping("{uid}/num")
    public Result applyNum(@PathVariable String uid,HttpServletRequest request){
        User operator=JwtUtils.isOperator(request,userService);
        ApplyNum applyNum=applyNumService.getById(uid);
        boolean success = applyNumService.pass(applyNum,operator);
        return success?Result.ok():Result.error();
    }

    /**
     * 管理员拒绝了某一次数申请
     */
    @PostMapping("{uid}/reject")
    public Result rejectNum(@PathVariable String uid,HttpServletRequest request){
        User operator=JwtUtils.isOperator(request,userService);
        ApplyNum applyNum=applyNumService.getById(uid);
        boolean success = applyNumService.reject(applyNum,operator);
        return success?Result.ok():Result.error();
    }

    /**
     * 获得待处理的次数申请列表
     */
    @GetMapping("num/list/{page}/{limit}")
    public Result numList(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<ApplyNum> userpage =applyNumService.getListyId(page,limit,user);
        return Result.ok().data(userpage).data("item",userpage.getRecords());
    }

    /**
     * 获得自己已经处理的次数申请列表
     */
    @GetMapping("num/history/{page}/{limit}")
    public Result numHistory(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<ApplyNum> userpage =applyNumService.getHistoryId(page,limit,user);
        return Result.ok().data(userpage).data("item",userpage.getRecords());
    }
}
